<?php
namespace App\Models\Relations;

use Illuminate\Support\Facades\DB;
use App\Models\Comments;
/**
 *
 * @author Joker
 *        
 */
trait CommentsTrait
{
    
    /**
     * 获取评论列表
     * @param  $appTable  
     * @param  $appId
     * @return array 数组
     */
    public function getCommentsList($appTable,$appId)
    {
        DB::connection()->setFetchMode(\PDO::FETCH_ASSOC);
        $commtents = DB::table('comments')->where(array('app_table'=>$appTable,'app_id'=>$appId,'to_comment_id'=>0))
                    ->orderBy('created_at','asc')
                    ->orderBy('comment_id','asc')
                    ->get();
        
        foreach ($commtents as &$comm)
        {
            $comm['child'] = DB::table('comments')->where(array('app_table'=>$appTable,'app_id'=>$appId,'to_comment_id'=>$comm['comment_id']))
                                        ->orderBy('created_at','asc')
                                        ->orderBy('comment_id','asc')
                                        ->get();
        }
        return $commtents;
    }
    
    /**
     * 获取评论列表
     * @param  $appTable  
     * @param  $appId
     * @return array
     */
    public function comments($appTable,$appId)
    {
         return   Comments::where(array('app_table'=>$appTable,'app_id'=>$appId,'to_comment_id'=>0))
                    ->orderBy('created_at','asc')
                    ->orderBy('comment_id','asc')
                    ->get();
    }
    
}
